//
//  LTChooseContentView.m
//  LearnTeacher
//
//  Created by 高刘通 on 2024/6/7.
//

#import "LTChooseContentView.h"
#import "LTChooseContentRightCell.h"
#import "LTChooseContentLeftCell.h"
#import "LTChooseContentLeftModel.h"
#import "LTChooseContentModel.h"

@interface LTChooseContentView()<UITableViewDelegate, UITableViewDataSource>
@property(strong, nonatomic) UITableView *leftTableView;
@property(strong, nonatomic) UITableView *contentTableView;
@property(strong, nonatomic) UIView *bottomView;
@end

@implementation LTChooseContentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.leftTableView];
        [self addSubview:self.contentTableView];
        [self addSubview:self.bottomView];
    }
    return self;
}

- (void)setLeftDatasource:(NSArray *)leftDatasource {
    _leftDatasource = leftDatasource;
    for (LTChooseContentLeftModel *model in self.leftDatasource) {
        model.lt_selected = NO;
    }
    if (self.leftDatasource.count > 0) {
        LTChooseContentLeftModel *model = self.leftDatasource[0];
        model.lt_selected = YES;
        self.contentDatasource = model.children;
    }
    [self.leftTableView reloadData];
}

- (void)setContentDatasource:(NSArray *)contentDatasource {
    _contentDatasource = contentDatasource;
    [self.contentTableView reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.leftTableView.frame = CGRectMake(0, 49, 105, 50 * 3);
    self.contentTableView.frame = CGRectMake(self.leftTableView.right, self.leftTableView.top, self.width - self.leftTableView.right, self.leftTableView.height + 40);
    self.bottomView.frame = CGRectMake(self.contentTableView.left, self.contentTableView.height - 38 + self.contentTableView.top, self.contentTableView.width, 38);
     
}

- (CGFloat)safeBottom {
    return [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.leftTableView) {
        return self.leftDatasource.count;
    }else {
        return self.contentDatasource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        LTChooseContentLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell" forIndexPath:indexPath];
        cell.model = self.leftDatasource[indexPath.row];
        return cell;
    }else {
        LTChooseContentRightCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell" forIndexPath:indexPath];
        cell.model = self.contentDatasource[indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        return 50;
    }else {
        LTChooseContentModel *model = self.contentDatasource[indexPath.row];
        if (model.cellHeight != 0) {
            return model.cellHeight; //缓存高度
        }
        CGFloat cellHeight = [self calculateHeightWithString:model.book_name];
        if (cellHeight < 20) {
            cellHeight = 20;
        }
        model.cellHeight = cellHeight + 30;
        return model.cellHeight;
    }
}
    
- (CGFloat)calculateHeightWithString:(NSString *)str {
    if (str.length < 0) {
        return 0;
    }
    CGRect rect = [str boundingRectWithSize:CGSizeMake(kScreenWidth - 105 - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium]} context:nil];
    CGFloat height = rect.size.height;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        for (LTChooseContentLeftModel *model in self.leftDatasource) {
            model.lt_selected = NO;
        }
        LTChooseContentLeftModel *model = self.leftDatasource[indexPath.row];
        model.lt_selected = YES;
        self.contentDatasource = model.children;
        
        [tableView reloadData];
    }else {
        if (self.didSelectHandler) {
            self.didSelectHandler(self.contentDatasource[indexPath.row]);
        }
    }
}

- (UITableView *)leftTableView {
    if (!_leftTableView) {
        _leftTableView = [self setupTableView];
        _leftTableView.backgroundColor = rgba(0, 0, 0, 0.05);
    }
    return _leftTableView;
}

- (UITableView *)contentTableView {
    if (!_contentTableView) {
        _contentTableView = [self setupTableView];
        _contentTableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
        _contentTableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 50, 0);
    }
    return _contentTableView;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.userInteractionEnabled = NO;
        _bottomView.backgroundColor = rgba(255, 255, 255, 0.8);
    }
    return _bottomView;
}

- (void)insertColorGradient {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = _bottomView.bounds;
    UIColor *startColor = [UIColor colorWithWhite:1.0 alpha:0.1];
    UIColor *endColor = [UIColor colorWithWhite:1.0 alpha:0.8];
    gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
    gradientLayer.startPoint = CGPointMake(0.0, 0.0);
    gradientLayer.endPoint = CGPointMake(0.0, 1);
    [_bottomView.layer addSublayer:gradientLayer];
}

- (UITableView *)setupTableView {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.estimatedRowHeight = 0;
    tableView.exclusiveTouch = YES;
    [tableView registerClass:[LTChooseContentLeftCell class] forCellReuseIdentifier:@"leftCell"];
    [tableView registerClass:[LTChooseContentRightCell class] forCellReuseIdentifier:@"rightCell"];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    return tableView;
}

@end
